package com.sharekitchen.controller;

import com.sharekitchen.common.R;
import com.sharekitchen.dto.DeviceDto;
import com.sharekitchen.dto.RoomDevDto;
import com.sharekitchen.dto.RoomDto;
import com.sharekitchen.entity.*;
import com.sharekitchen.mapper.DeviceMapper;
import com.sharekitchen.service.DeviceService;
import com.sharekitchen.service.RoomService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Slf4j
@RestController
@RequestMapping("/room")
@CrossOrigin
public class RoomController {

    @Autowired
    private RoomService roomService;
    @Autowired
    private DeviceService deviceService;

    @GetMapping("/all")
    public R<List<RoomDto>> getAllRoom() {
        HashMap<String, RoomDto> hashMap = new HashMap<>();
        List<Room> allRoom = roomService.findAllRoom();
        if (allRoom == null) {
            return R.fail("该房间不存在");
        }
        for (int i = 0; i < allRoom.size(); i++) {
            Room room = allRoom.get(i);
            RoomDto roomDto = new RoomDto();
            roomDto.setRoomId(room.getRoomId());
            roomDto.setStar(room.getStar());
            roomDto.setRoomStatus(room.getRoomStatus());
            roomDto.setIsDelete(room.getIsDelete());
            roomDto.setRoomDevices(roomService.findRoomDevById(room.getRoomId()));
            hashMap.put(roomDto.getRoomId(), roomDto);
            System.out.println("0" + roomDto);
        }
        List<RoomAddress> allRoomAddr = roomService.findAllRoomAddr();
        if (allRoomAddr == null) {
            return R.fail("该房间不存在");
        }
        for (int i = 0; i < allRoomAddr.size(); i++) {
            hashMap.get(allRoomAddr.get(i).getRoomId()).setRoomAddress(allRoomAddr.get(i));
        }
        List<RoomImages> allRoomImages = roomService.findAllRoomImages();
        if (allRoomImages == null) {
            return R.fail("该房间不存在");
        }
        for (int i = 0; i < allRoomImages.size(); i++) {
            hashMap.get(allRoomImages.get(i).getRoomId()).setRoomImages(allRoomImages.get(i));
        }
        System.out.println("1" + hashMap);
        System.out.println("2" + hashMap.values());

        List<RoomDto> roomDtos = new ArrayList<>(hashMap.values());
        if (roomDtos != null) {
            return R.ok("查询成功", roomDtos);
        }
        return R.error("查询失败");
    }

    @GetMapping("/id")
    public R<RoomDto> getAllRoomById(@RequestParam String roomId) {
        Room room = roomService.findRoomById(roomId);
        if (room == null) {
            return R.fail("查询失败");
        }
        RoomAddress roomAddrById = roomService.findRoomAddrById(roomId);
        if (roomAddrById == null) {
            return R.fail("查询失败");
        }
        List<RoomDevice> roomDevs = roomService.findRoomDevById(roomId);
        if (roomDevs == null) {
            return R.fail("查询失败");
        }
        RoomImages roomImagesById = roomService.findRoomImagesById(roomId);
        if (roomImagesById == null) {
            return R.fail("查询失败");
        }
        RoomDto roomDto = new RoomDto();
        roomDto.setRoomDevices(roomDevs);
        roomDto.setRoomAddress(roomAddrById);
        roomDto.setRoomImages(roomImagesById);
        roomDto.setRoomId(room.getRoomId());
        roomDto.setStar(room.getStar());
        roomDto.setRoomStatus(room.getRoomStatus());
        roomDto.setIsDelete(room.getIsDelete());

        if (roomDto != null) {
            return R.ok("查询成功", roomDto);
        }

        return R.error("房间不存在");
    }

//    新增房间
    @PostMapping("/add")
    public R<String> addRoom(@RequestBody RoomDevDto roomDevDto) {
        if (roomService.addRoom(roomDevDto)){
            return R.ok("添加成功",null);
        }
        return R.error("添加失败");
    }

//    删除房间
    @DeleteMapping("/delete")
    public R<String> deleteRoomById(@RequestParam String roomId){
        if (roomService.findRoomById(roomId) == null){
            return R.fail("该房间不存在");
        }
        if (roomService.delRoom(roomId)){
            return R.ok("删除成功",null);
        }
        return R.error("删除失败");
    }

//    修改房间
    @PutMapping("/change")
    public R<String> updateRoom(@RequestBody RoomDto roomDto){
        return R.error("修改失败");
    }
//  根据房间id查询房间设备信息
    @GetMapping("/device")
    public R<List<Device>> findDeviceByRoomId(@RequestParam String roomId){
        List<Device> device = new ArrayList<>();
        for (RoomDevice roomDevice :roomService.findRoomDevById(roomId)) {
            device.add(deviceService.getDevById(roomDevice.getDevId()));
        }
        if (deviceService==null){
            return R.fail("暂无该房间设备信息");
        }
        if (deviceService!=null){
            return R.ok("查询成功",device);
        }
        return R.error("出错了");
    }

}
